package andriod.action.task;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import andriod.service.ArdTaskService;

import com.jcrj.action.BaseAction;
import com.jcrj.entity.OFile;
import com.jcrj.entity.OMyOpinion;
import com.jcrj.entity.OTask;
import com.jcrj.entity.OUser;
import com.jcrj.service.FileService;
import com.jcrj.service.SysLogService;
import com.jcrj.service.TaskService;
import com.jcrj.util.DateProcessor;

public class ArdTaskAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private ArdTaskService ardTaskService;
	private TaskService taskService;
	private SysLogService sysLogService;
	private FileService fileService;
	private OTask task;
	private OMyOpinion opinion;
	private String[] taskIds;
	private int taskId;
	private String status;

	public String taskHome() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			JSONObject obj = new JSONObject();
			String userId = this.sysLogService.getAgentUser(user);
			long count = this.ardTaskService.countTodoTask(userId);
			obj.put("todoCount", Long.valueOf(count));
			count = this.ardTaskService.countUserTask(user.getId().intValue());
			obj.put("peCount", Long.valueOf(count));
			count = this.ardTaskService.countNotifyTask(user.getId().intValue());
			obj.put("notifyCount", Long.valueOf(count));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getTodoTaskList() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			String userId = this.sysLogService.getAgentUser(user);
			List<OTask> list = this.ardTaskService.getTodoTaskList(userId);
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			JSONArray obj = JSONArray.fromObject(list, config);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getUserTaskList() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			List<OTask> list = this.ardTaskService.getUserTaskList(user.getId().intValue());
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			JSONArray obj = JSONArray.fromObject(list, config);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getNotifyTaskList() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			List<OTask> list = this.ardTaskService.getNotifyTaskList(user.getId().intValue());
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			JSONArray obj = JSONArray.fromObject(list, config);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String toTaskAdd() {
		try {
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String taskAdd() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			this.task.setUserId(user.getId());
			this.task.setUserName(user.getName());
			this.task.setCreationTime(new Date());
			this.taskService.saveTase(this.task, this.task.getNotifyUserId());
			this.sysLogService.saveLog("日程安排", user.getName(), "添加了“" + this.task.getTitle() + "”日程");
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String toTaskUpdate() {
		try {
			OTask task = this.taskService.getTask(this.taskId);
			String notifyId = task.getNotifyUserId();
			String name = "";
			if ((notifyId != null) && (!"".equals(notifyId))) {
				String[] userIds = notifyId.split(",");
				Map<Integer, OUser> map = new HashMap<Integer, OUser>();
				List<OUser> list = this.taskService.getUserName(null, notifyId);
				for (int i = 0; i < list.size(); i++) {
					OUser u = (OUser) list.get(i);
					map.put(u.getId(), u);
				}
				for (int i = 0; i < userIds.length; i++) {
					name = name + ((OUser) map.get(Integer.valueOf(Integer.parseInt(userIds[i])))).getName();
					if (i != userIds.length - 1) {
						name = name + ",";
					}
				}
			}
			task.setName(name);
			List<OFile> list = this.fileService.getFileList(task.getFileId());
			JSONObject obj = new JSONObject();
			obj.put("object", task);
			obj.put("fileList", list);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String taskUpdate() {
		try {
			this.taskService.updateTask(this.task);
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String deleteTask() {
		try {
			this.taskService.deleteTask(this.taskIds);
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String taskNew() {
		try {
			OTask task = this.taskService.getTask(this.taskId);
			String notifyId = task.getNotifyUserId();
			String name = "";
			if ((notifyId != null) && (!"".equals(notifyId))) {
				String[] userIds = notifyId.split(",");
				Map<Integer, OUser> map = new HashMap<Integer, OUser>();
				List<OUser> list = this.taskService.getUserName(null, notifyId);
				for (int i = 0; i < list.size(); i++) {
					OUser u = (OUser) list.get(i);
					map.put(u.getId(), u);
				}
				for (int i = 0; i < userIds.length; i++) {
					name = name + ((OUser) map.get(Integer.valueOf(Integer.parseInt(userIds[i])))).getName();
					if (i != userIds.length - 1) {
						name = name + ",";
					}
				}
			}
			task.setName(name);
			List<OFile> fileList = this.fileService.getFileList(task.getFileId());
			List<OMyOpinion> opinionList = this.taskService.getopinTask(this.taskId);
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			JSONObject o1 = JSONObject.fromObject(task, config);
			JSONArray o2 = JSONArray.fromObject(opinionList, config);
			JSONObject obj = new JSONObject();
			obj.put("object", o1.toString());
			obj.put("fileList", fileList);
			obj.put("opinionList", o2.toString());
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String addOpinionTask() {
		try {
			OUser ou = (OUser) super.getSession().get("userInfo");
			this.opinion.setUserName(ou.getName());
			this.opinion.setUserId(ou.getId());
			this.opinion.setAddTime(new Date());
			this.taskService.updateTaskStatus(this.status, this.opinion.getTaskId().intValue());
			this.taskService.saveOpinionTask(this.opinion);
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public ArdTaskService getArdTaskService() {
		return this.ardTaskService;
	}

	public void setArdTaskService(ArdTaskService ardTaskService) {
		this.ardTaskService = ardTaskService;
	}

	public TaskService getTaskService() {
		return this.taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public SysLogService getSysLogService() {
		return this.sysLogService;
	}

	public void setSysLogService(SysLogService sysLogService) {
		this.sysLogService = sysLogService;
	}

	public FileService getFileService() {
		return this.fileService;
	}

	public void setFileService(FileService fileService) {
		this.fileService = fileService;
	}

	public OTask getTask() {
		return this.task;
	}

	public void setTask(OTask task) {
		this.task = task;
	}

	public OMyOpinion getOpinion() {
		return this.opinion;
	}

	public void setOpinion(OMyOpinion opinion) {
		this.opinion = opinion;
	}

	public String[] getTaskIds() {
		return this.taskIds;
	}

	public void setTaskIds(String[] taskIds) {
		this.taskIds = taskIds;
	}

	public int getTaskId() {
		return this.taskId;
	}

	public void setTaskId(int taskId) {
		this.taskId = taskId;
	}

	public String getStatus() {
		return this.status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
}